﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Yonkly.Utility
{
    public static class Globals
    {
        public const int PageSize = 20;

        public static string SiteUrl = ConfigurationManager.AppSettings["SiteUrl"];
        public static string AmazonBucketName = ConfigurationManager.AppSettings["AmazonBucketName"];
        public static readonly Regex EmailExpression =
            new Regex(
                @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                RegexOptions.Compiled | RegexOptions.Singleline);

        public static bool DisableSignup
        {
            get { return Convert.ToBoolean(ConfigurationManager.AppSettings["DisableSignup"]); }
        }

        public static string AmazonAccessKeyId
        {
            get { return ConfigurationManager.AppSettings["AmazonAccessKeyId"]; }
        }

        public static string AmazonSecretAccessKey
        {
            get { return ConfigurationManager.AppSettings["AmazonSecretAccessKey"]; }
        }

        public static bool IsValidEmail(string email)
        {
            return EmailExpression.IsMatch(email);
        }

        public static bool IsValidUsername(HttpContextBase ctx, string username)
        {
            username = username.ToLower();
            if (username.Length < 4)
                return false;
            var UsernameExpression = new Regex(@"^[a-zA-Z0-9_]+$",
                                               RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var valid = UsernameExpression.IsMatch(username);
            if (!valid)
                return false;

            //make sure the username doesn't match any of the routes.
            foreach (Route route in RouteTable.Routes)
            {
                if (username == route.Defaults["controller"].ToString().ToLower())
                    return false;
                if (username == route.Defaults["action"].ToString().ToLower())
                    return false;
            }
            return true;
        }
    }
}